﻿using System;
using System.IO;
using System.Collections.Generic;

namespace RandomSelect
{
    class ConfigSetting
    {
        public Dictionary<string, string> ReadConfig(string path)
        {
            FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            string fileContain = "";
            int argIndex = 0;
            string[] argsArr = new string[7];
            byte[] chars = new byte[200];
            Dictionary<string, string> argsDic = new Dictionary<string, string>();

            file.Read(chars, 0, 200);

            //若文件不存在则主动写入
            if (chars[0] == 0)
            {
                string inString = "SingleIndex1=1\nSingleIndex2=51\nDoubleIndex1=1\nDoubleIndex2=9\nDoubleIndex3=1\nDoubleIndex4=6\nWallpaper=true";
                for (int i = 0; i < inString.Length; i++)
                {
                    file.WriteByte((byte)inString[i]);
                }
            }

            //读取参数
            for (int i = 0; ; i++)
            {
                if (chars[i] == 0) break;
                if (!(chars[i] >= 32 && chars[i] <= 126))
                {
                    if (chars[i] == 10) argIndex++;
                    if (argIndex >= argsArr.Length) break;
                }
                else
                {
                    if (chars[i].ToString() != "")
                    {
                        fileContain += (char)chars[i];
                        argsArr[argIndex] += (char)chars[i];
                    }
                }
            }
            file.Close();

            for (int i = 0; i < argsArr.Length; i++)
            {
                argsDic.Add(argsArr[i].Split('=')[0], argsArr[i].Split('=')[1]);
            }
            return argsDic;
        }

        public void WriteConfig(string path, string config)
        {
            string inString = config;
            FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write);
            for (int i = 0; i < inString.Length; i++)
            {
                file.WriteByte((byte)inString[i]);
            }
        }
    }
}
